package com.yishusmt.mobsmt.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 获取用户操作数据
 *
 * @author zhangyi
 * @date 2018/8/9 15:24
 */
@Aspect
@Component
@Slf4j
public class AopConfig {

    @Pointcut("execution(* com.yishusmt.mobsmt.controller..*.*(..)))")
    public void pointCut() {
    }

    @AfterReturning(pointcut = "pointCut()", returning = "returnValue")
    public void operate(JoinPoint joinPoint, Object returnValue) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

    }
    /**
     * 获取当前请求的IP地址
     *
     * @param request
     * @return
     */
    public static String getLocalIp(HttpServletRequest request) {

        String ip="";
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        }else{
            ip = request.getHeader("x-forwarded-for");
        }
        if(ip.length()>15){
            if(ip.indexOf(",")>0){
                ip = ip.substring(0,ip.indexOf(","));
            }
        }
        return ip;
    }
}
